
using System;

using Grr;
using Grr.Web;
using Grr.Models;

using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Views.Aspx;

public partial class Views_Grurrah_create : GrurrahViewPage {

    protected override string TabName
    {
        get { return "create"; }
    }
    protected override void SetProperties ()
    {
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        string name = null;
        string tainted_name = GrurrahNameTextBox.Text;
        string tainted_description = FCKeditor1.Value;

        ClearErrorMessages();

        if (!Validator.IsValidGrurrahName(tainted_name))
        {
            GrurrahNameErrorImage.Visible = true;
            ErrorLabel.Text = "Grurrah name is not valid.";
            ErrorLabel.Visible = true;
            return;
        } else {
            name = StringUtil.Clean (tainted_name.Trim ());
        }

        if (Grurrah.QueryForByName(tainted_name) != null)
        {
            GrurrahNameErrorImage.Visible = true;
            ErrorLabel.Text = "A Grurrah with this name already exists.";
            ErrorLabel.Visible = true;
            return;
        }

        if (!Grurrah.Create(name, tainted_description))
        {
            ErrorLabel.Text = "Unable to create Grurrah.";
            ErrorLabel.Visible = true;
            return;
        }

        Response.Redirect (ResolveUrl (String.Concat ("~/grurrahs/", StringUtil.ToFriendlyUri (name))), false);
    }

    private void ClearErrorMessages()
    {
        GrurrahNameErrorImage.Visible = false;
        ErrorLabel.Visible = false;
    }
}
